package com.mini.service;

import com.mini.conditon.OperationRecordCondition;
import com.mini.model.BusinessOperationRecord;
import com.mini.repository.BusinessOperationRecordRepository;
import com.mini.vo.PageResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: zhaocg
 * @created: 2021/01/20 13:57
 */
@Slf4j
@Service
public class BusinessOperationRecordService extends BaseService {



    @Autowired
    private BusinessOperationRecordRepository operationRecordRepository;


    /**
     * 分页查询操作记录
     *
     * @param condition
     * @return
     */
    public PageResultVO<BusinessOperationRecord> page(OperationRecordCondition condition){
        PageResultVO<BusinessOperationRecord> resultVO = new PageResultVO<>();

        Pageable pageable = buildPageRequest(condition.getPageNumber(),condition.getPageSize(),new Sort(Sort.Direction.DESC,"operationTime"));
        Page<BusinessOperationRecord> page = operationRecordRepository.findAll(getStudentSpecification(condition), pageable);
        resultVO.setTotal(page.getTotalElements());
        resultVO.setRows(decryptObjs(page.getContent()));
        return resultVO;
    }

    private Specification<BusinessOperationRecord> getStudentSpecification(OperationRecordCondition condition){
        if(StringUtils.isNotBlank(condition.getPhoneNo())){
            condition.setPhoneNo(encryptField(condition.getPhoneNo()));
        }

        return new Specification<BusinessOperationRecord>() {
            @Override
            public Predicate toPredicate(Root<BusinessOperationRecord> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();

                if(StringUtils.isNotBlank(condition.getPhoneNo())){
                    predicates.add(criteriaBuilder.equal(root.get("phoneNo").as(String.class),condition.getPhoneNo()));
                }
                if(condition.getLessonSiteId() != null && 0 != condition.getLessonSiteId().intValue()){
                    predicates.add(criteriaBuilder.equal(root.get("lessonSiteId").as(Long.class),condition.getLessonSiteId()));
                }
                if(condition.getOperationSource() != null){
                    predicates.add(criteriaBuilder.equal(root.get("operationSource").as(Integer.class),condition.getOperationSource()));
                }
                if(condition.getImportantState() != null){
                    predicates.add(criteriaBuilder.equal(root.get("importantState").as(Integer.class),condition.getImportantState()));
                }

                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
    }

    /**
     * 根据手机号、站点、操作类型查询数据
     *
     * @param phoneNo
     * @param lessonSiteId
     * @param operationTypes
     * @return
     */
    public List<BusinessOperationRecord> find(String phoneNo, Long lessonSiteId, List<Integer> operationTypes){
        String phone = encryptField(phoneNo);
        return operationRecordRepository.findAllByPhoneNoAndLessonSiteIdAndAndOperationTypeIn(phone,lessonSiteId,operationTypes);
    }

    /**
     * 保存操作记录
     *
     * @param studentOperationInfo
     */
    public void save(BusinessOperationRecord studentOperationInfo){
        studentOperationInfo = (BusinessOperationRecord) encryptObj(studentOperationInfo);
        operationRecordRepository.save(studentOperationInfo);
    }

    /**
     * 设置记录属性并保存
     */
    public void save(Long lessonSiteId, String phoneNo, Integer operationSource, String operationRecord,
                      Long operationUserId, String operationUserName, Integer operationType, String operationLoginName,
                      String roleName, Date operateTime){
        BusinessOperationRecord studentOperationInfo = new BusinessOperationRecord();
        studentOperationInfo.setLessonSiteId(lessonSiteId);
        studentOperationInfo.setPhoneNo(phoneNo);
        studentOperationInfo.setOperationSource(operationSource);
        studentOperationInfo.setOperationRecord(operationRecord);
        studentOperationInfo.setOperationUserId(operationUserId);
        studentOperationInfo.setOperationUserName(operationUserName);
        studentOperationInfo.setOperationTime(operateTime == null ? new Date() : operateTime);
        studentOperationInfo.setOperationType(operationType);
        studentOperationInfo.setOperationLoginName(operationLoginName);
        studentOperationInfo.setRoleName(roleName);
        if(1 == operationType.intValue()){
            studentOperationInfo.setImportantState(1);
        }
        save(studentOperationInfo);
    }

    /**
     * 根据ID获取操作记录
     * @param id
     * @return
     */
    public BusinessOperationRecord findById(Long id){
        BusinessOperationRecord operationInfo = operationRecordRepository.findOne(id);
        operationInfo = (BusinessOperationRecord) decryptObj(operationInfo);
        return operationInfo;
    }

}
